package com.yuda.tools.detection.controller;

import com.yuda.tools.detection.tool.IdAndAnswer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class UpdateQcAnswer {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 扶正答案
     */
    @GetMapping("/updateQcAnswer")
    public synchronized  String updateFormalAnswer() {
        StringBuffer sb = new StringBuffer();
        String sql = "SELECT id,answer from mq_answer_qc_copy1 where ques_id not in ('175','286','139','140')";
        BeanPropertyRowMapper<IdAndAnswer> idAndAnswerBeanPropertyRowMapper = new BeanPropertyRowMapper<>(IdAndAnswer.class);
        List<IdAndAnswer> list = namedParameterJdbcTemplate.query(sql, idAndAnswerBeanPropertyRowMapper);
        System.out.println("list --> " + list.size());
        int threadNum = 100;
        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
        ConcurrentLinkedDeque<IdAndAnswer> idAndAnswers = new ConcurrentLinkedDeque<>(list);
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        AtomicInteger atomicInteger = new AtomicInteger(0);
        for (int i = 0; i < threadNum; i++) {
            executorService.execute(() -> {
                while (!idAndAnswers.isEmpty()) {
                    IdAndAnswer idAndAnswer = idAndAnswers.pollFirst();
                    String answer = idAndAnswer.getAnswer();
                    if (answer.equals("-1") || answer.equals("-2")) {
                        continue;
                    }
                    String[] split = answer.split("#");
                    Arrays.sort(split);
                    String newAnswer = String.join("#", split);
                    jdbcTemplate.update("update mq_answer_qc_copy1 set answer = ? where id = ? ", newAnswer, idAndAnswer.getId());
                    System.out.println("正在处理第->" + atomicInteger.incrementAndGet());
                }
                countDownLatch.countDown();
            });
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("完毕。。。");
        sb.append("扶正完毕");
        executorService.shutdown();
        return sb.toString();
    }

}
